<template>
  <div class="approval">
    <el-card class="box-card" style="margin-top: 110px;">
      <div class="head-title2">
        <div class='title'>
          {{$t('achievements.title1')}}
        </div>
        <div class="info">
          <el-tooltip placement="left">
            <div slot="content">
              {{define}}
            </div>
            <div class="btns"></div>
          </el-tooltip>
        </div>
      </div>
      <div class="menu-search">
        <div class="menu-title">
          <!-- 展期时间 -->
          <div class="searchItem searchItem-date">
            <span>{{$t('ExtendStatement.ExtensionTime')}}</span>
            <el-date-picker
              size="small"
              v-model="extDate"
              type="daterange"
              :range-separator="$t('btn.go')"
              :start-placeholder="$t('rule.beginDate')"
              :end-placeholder="$t('rule.endDate')"
              value-format="yyyy-MM-dd"
              @change="dateChangeTime"
            ></el-date-picker>
          </div>
          <!-- 业务ID -->
          <div class="searchItem">
            <span>{{$t('approval.orderId')}}</span>
            <el-input v-model="listQuery.orderId" :placeholder="$t('rule.orderId')" size="small"></el-input>
          </div>
          <div class="menu-title-btn">
            <button type="button" @click="search">{{$t('btn.query')}}</button>
            <button type="button" @click="reset">{{$t('btn.reset')}}</button>
            <button
              type="button"
              @click="handleDownload"
              :disabled="expDis"
              v-if="extendReport_btn_export"
            >{{$t('btn.export')}}</button>
          </div>
        </div>
      </div>
    </el-card>
    <el-card class="box-card" style="margin-top: 10px;margin-bottom: 20px;">
      <div class="title">{{$t('DailyStatistics.ListDetails')}}</div>
      <el-table
        :sum-text="$t('ApprReport.total')"
        v-loading="listLoading"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        border
        size="mini"
      >
        <!-- 业务ID -->
        <el-table-column
          align="center"
          header-align="center"
          prop="orderId"
          :label="$t('approval.orderId')"
        ></el-table-column>
        <!-- 展期前状态 -->
        <el-table-column
          align="center"
          header-align="center"
          prop="extendedBeforeBillStatus"
          :label="$t('ExtendStatement.PreExtstatus')"
        >
          <template slot-scope="scope">
            <span
              v-if="scope.row.extendedBeforeBillStatus === 1"
            >{{$t('approvalDetail.repayments')}}</span>
            <span v-if="scope.row.extendedBeforeBillStatus === 2">{{$t('processing.result4')}}</span>
            <span v-if="scope.row.extendedBeforeBillStatus === 3">{{$t('approvalDetail.status4')}}</span>
            <span v-if="scope.row.extendedBeforeBillStatus === 4">{{$t('approval.clear')}}</span>
            <span v-if="scope.row.extendedBeforeBillStatus === 5">{{$t('approvalDetail.blankOut')}}</span>
          </template>
        </el-table-column>
        <!-- 展期后状态 -->
        <el-table-column
          align="center"
          header-align="center"
          prop="extendedAfterBillStatus"
          :label="$t('ExtendStatement.PostExtstatus')"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.extendedAfterBillStatus === 1">{{$t('approval.audit')}}</span>
            <span v-if="scope.row.extendedAfterBillStatus === 2">{{$t('approval.credit')}}</span>
            <span v-if="scope.row.extendedAfterBillStatus === 3">{{$t('approval.repayments')}}</span>
            <span v-if="scope.row.extendedAfterBillStatus === 4">{{$t('approval.refused')}}</span>
            <span v-if="scope.row.extendedAfterBillStatus === 5">{{$t('approval.clear')}}</span>
            <span v-if="scope.row.extendedAfterBillStatus === 6">{{$t('approvalDetail.status4')}}</span>
            <span v-if="scope.row.extendedAfterBillStatus === 7">{{$t('approvalDetail.status5')}}</span>
            <span v-if="scope.row.extendedAfterBillStatus === 8">{{$t('approvalDetail.status6')}}</span>
            <span v-if="scope.row.extendedAfterBillStatus === 9">{{$t('approvalDetail.blankOut')}}</span>
          </template>
        </el-table-column>
        <!-- 逾期天数 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="overdueDay"
          :label="$t('processing.overDay')"
        ></el-table-column>
        <!-- 订单金额 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="lentAmt"
          :formatter="cashFormatter"
          :label="$t('ExtendStatement.orderAmt')"
        ></el-table-column>
        <!-- 展期费用 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="extendedAmt"
          :formatter="cashFormatter"
          :label="$t('expireRemind.extensionFee')"
        ></el-table-column>
        <!-- 订单当前状态 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="orderStatus"
          :label="$t('ExtendStatement.OrderCurrentStatus')"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.orderStatus === 1">{{$t('approvalDetail.repayments')}}</span>
            <span v-if="scope.row.orderStatus === 2">{{$t('processing.result4')}}</span>
            <span v-if="scope.row.orderStatus === 3">{{$t('approvalDetail.status4')}}</span>
            <span v-if="scope.row.orderStatus === 4">{{$t('approval.clear')}}</span>
            <span v-if="scope.row.orderStatus === 5">{{$t('approvalDetail.blankOut')}}</span>
          </template>
        </el-table-column>
        <!-- 展期时间 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="extendedDate"
          :label="$t('ExtendStatement.ExtensionTime')"
        ></el-table-column>
        <!-- 展期后到期时间 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="lastPayDate"
          :label="$t('ExtendStatement.ExtAfterDueTime')"
        ></el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { padLeftZero, formatCash } from "utils/index";
import {
  page,
  pageExport
} from "api/reportForm/OperationReport/ExtendStatement/index";
export default {
  data() {
    return {
      define: '',
      extDate: [new Date(), new Date()],
      extendReport_btn_export: false,
      listLoading: false,
      total: null,
      expDis: true,
      listQuery: {
        limit: 5,
        page: 1,
        startDate: "",
        endDate: "",
        orderId: ""
      },
      tableData: []
    };
  },
  computed: {
    ...mapGetters(["elements", "ExtendStatementPage", 'getReportDefine'])
  },
  created() {
    if (this.ExtendStatementPage.has(this.$route.path)) {
      this.listQuery = this.ExtendStatementPage.get(this.$route.path);
    }
    this.getTime();
    this.getList();
    this.extendReport_btn_export = this.elements["extendReport:btn_export"];
    // 获取报表定义
    this.getReportDefine.forEach(item => {
      if (item.dictCode === 'EXTEND_REPORT') {
        this.define = item.dictValue
      }
    })
  },
  methods: {
    // 日期
    dateChangeTime(val) {
      if (val === undefined || val === null || val === "") {
        this.listQuery.startDate = "";
        this.listQuery.endDate = "";
      } else {
        this.listQuery.startDate = val[0];
        this.listQuery.endDate = val[1];
      }
    },
    // 金额加分隔符
    cashFormatter(row, col, cellValue) {
      return formatCash(cellValue);
    },
    getTime() {
      this.listQuery.startDate = this.GetDateStr(0);
      this.listQuery.endDate = this.GetDateStr(0);
    },
    GetDateStr(AddDayCount) {
      const dd = new Date();
      dd.setDate(dd.getDate() + AddDayCount); // 获取AddDayCount天后的日期
      const y = dd.getFullYear();
      const m = padLeftZero(dd.getMonth() + 1, 2);
      const d = padLeftZero(dd.getDate(), 2);
      return y + "-" + m + "-" + d;
    },
    // 点击导出
    handleDownload() {
      this.listLoading = true;
      pageExport(this.listQuery).then(res => {
        res.data.rows.forEach(item => {
          // 展期前状态
          switch (item.extendedBeforeBillStatus) {
            case 1:
              item.extendedBeforeBillStatus = this.$t(
                "approvalDetail.repayments"
              );
              break;
            case 2:
              item.extendedBeforeBillStatus = this.$t("processing.result4");
              break;
            case 3:
              item.extendedBeforeBillStatus = this.$t("approvalDetail.status4");
              break;
            case 4:
              item.extendedBeforeBillStatus = this.$t("approval.clear");
              break;
            case 5:
              item.extendedBeforeBillStatus = this.$t(
                "approvalDetail.blankOut"
              );
              break;
          }
          // 展期后状态
          switch (item.extendedAfterBillStatus) {
            case 1:
              item.extendedAfterBillStatus = this.$t("approval.audit");
              break;
            case 2:
              item.extendedAfterBillStatus = this.$t("approval.credit");
              break;
            case 3:
              item.extendedAfterBillStatus = this.$t("approval.repayments");
              break;
            case 4:
              item.extendedAfterBillStatus = this.$t("approval.refused");
              break;
            case 5:
              item.extendedAfterBillStatus = this.$t("approval.clear");
              break;
            case 6:
              item.extendedAfterBillStatus = this.$t("approvalDetail.status4");
              break;
            case 7:
              item.extendedAfterBillStatus = this.$t("approvalDetail.status5");
              break;
            case 8:
              item.extendedAfterBillStatus = this.$t("approvalDetail.status6");
              break;
            case 9:
              item.extendedAfterBillStatus = this.$t("approvalDetail.blankOut");
              break;
          }
          // 订单当前状态
          switch (item.orderStatus) {
            case 1:
              item.orderStatus = this.$t("approvalDetail.repayments");
              break;
            case 2:
              item.orderStatus = this.$t("processing.result4");
              break;
            case 3:
              item.orderStatus = this.$t("approvalDetail.status4");
              break;
            case 4:
              item.orderStatus = this.$t("approval.clear");
              break;
            case 5:
              item.orderStatus = this.$t("approvalDetail.blankOut");
              break;
          }
        });
        require.ensure([], () => {
          const { export_json_to_excel } = require("@/vendor/Export2Excel");
          const tHeader = [
            this.$t("approval.orderId"),
            this.$t("ExtendStatement.PreExtstatus"),
            this.$t("ExtendStatement.PostExtstatus"),
            this.$t("processing.overDay"),
            this.$t("ExtendStatement.orderAmt"),
            this.$t("expireRemind.extensionFee"),
            this.$t("ExtendStatement.OrderCurrentStatus"),
            this.$t("ExtendStatement.ExtensionTime"),
            this.$t("ExtendStatement.ExtAfterDueTime")
          ];
          const filterVal = [
            "orderId",
            "extendedBeforeBillStatus",
            "extendedAfterBillStatus",
            "overdueDay",
            "lentAmt",
            "extendedAmt",
            "orderStatus",
            "extendedDate",
            "lastPayDate"
          ];
          const list = res.data.rows;
          const data = this.formatJson(filterVal, list);
          export_json_to_excel(tHeader, data, this.$t("route.ExtendStatement"));
          this.listLoading = false;
        });
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]));
    },
    getList() {
      this.listLoading = true;
      this.$store.dispatch("saveExtendStatementPage", {
        path: this.$route.path,
        listQuery: this.listQuery
      });
      page(this.listQuery).then(res => {
        res.data.rows.forEach((item, index) => {
          item.index =
            index + (this.listQuery.page - 1) * this.listQuery.limit + 1;
        });
        this.expDis = false;
        this.listLoading = false;
        this.tableData = res.data.rows;
        this.total = Number(res.data.total);
      });
    },
    //查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    //重置
    reset() {
      this.listQuery.limit = 5;
      this.listQuery.page = 1;
      this.extDate = [new Date(), new Date()];
      this.listQuery.startDate = this.GetDateStr(0);
      this.listQuery.endDate = this.GetDateStr(0);
      this.listQuery.orderId = "";
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.box-card {
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
}
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before {
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 220px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  margin-left: 20px;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.head-title2 {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.btns {
  display: inline-block;
  width: 24px;
  height: 24px;
  background-size: cover;
  background-image: url("../../../../assets/images/question-mark.svg");
}
</style>